package dao

import (
	//"fmt"
	"phoenix/gin_web/model"

	"gorm.io/gorm"
)

type UserDao struct {
	db *gorm.DB
}

func NewUserDao(db *gorm.DB) *UserDao {
	//自动去做migrate
	db.AutoMigrate(&model.User{})
	return &UserDao{
		db: db,
	}
}

func (dao *UserDao) GetUserByID(id uint) (*model.User, bool) {
	u := &model.User{}
	dao.db.Where("id = ?", id).Find(u)
	return u, u.ID != 0
}

func (dao *UserDao) IsTelephoneExist(user *model.User) (*model.User, bool) {
	dao.db.Where("telephone = ?", user.Telephone).Find(&user)
	return user, user.ID != 0
}

func (dao *UserDao) CreateNewUser(user *model.User) error {
	return dao.db.Create(user).Error
}